package zmee.pmct.formplugin.pmas;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.org.OrgServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;

import java.util.*;

import static kd.bos.orm.query.QCP.equals;

/**
 * @author yss
 * @createTime 2022-11-16 10:29
 * @description 联系单表单插件
 */
public class ContactBillPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener {

    /** 组织 */
    private final static String ORG_FIELD = "zmee_orgfield";
    /** 行政组织 */
    private final static String ADMIN_ORG_FIELD = "zmee_adminorg";
    /** 支出合同 */
    private final static String OUT_CONTRACT_FIELD = "zmee_basedatafield";
    /** 项目 */
    private final static String PROJECTNAME_FIELD = "projectname";
    /** 对方单位 */
    private final static String PARTB_ORG_FIELD = "zmee_textfield";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);

        BasedataEdit fieldEdit2 = this.getView().getControl(OUT_CONTRACT_FIELD);
        fieldEdit2.addBeforeF7SelectListener(this);
        BasedataEdit basedataEdit = this.getView().getControl(PROJECTNAME_FIELD);
        basedataEdit.addBeforeF7SelectListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        //1、创建表单的时候字段带出当前登录人的组织
        long userId = UserServiceHelper.getCurrentUserId();
//        2、获取当前登入人公司
        UserServiceHelper.getUserDefaultOrgID(userId);
//        3、获取当前登入人部门
        @SuppressWarnings("deprecation")
        Map<String, Object> mapOrg = OrgServiceHelper.getCompanyfromOrg(UserServiceHelper.getUserDefaultOrgID(userId));

        this.getModel().setValue(ORG_FIELD, UserServiceHelper.getUserDefaultOrgID(userId));

    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IDataEntityProperty property = e.getProperty();
        ChangeData changeData = e.getChangeSet()[0];
        Object newValue = changeData.getNewValue();
        Object oldValue = changeData.getOldValue();
        String name = property.getName();
        if(OUT_CONTRACT_FIELD.equals(name)){//支出合同
            propertyChanged_OutContract(e,newValue);
        }else if(PROJECTNAME_FIELD.equals(name)){
            propertyChanged_Project(e,newValue);
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent evt) {
        String name = evt.getProperty().getName();
        if (OUT_CONTRACT_FIELD.equals(name)) {
            beforeF7Select_OutContract(evt);
        } else if (PROJECTNAME_FIELD.equals(name)) {
            ListShowParameter showParameter = (ListShowParameter) evt.getFormShowParameter();
            DynamicObject org = (DynamicObject) this.getModel().getValue(ORG_FIELD);
            if (org != null) {
                QFilter qFilter = new QFilter("useorg.id", equals, org.getLong("id"));
                //根据组织过滤
                showParameter.getListFilterParameter().setFilter(qFilter);
            }
        }
    }

    /**
     * 支出合同值更新
     * @param e
     * @param newValue
     */
    protected void propertyChanged_OutContract(PropertyChangedArgs e,Object newValue){
        String partbOrgName = "";
        if(newValue != null){//不为空的情况下，需要重新给项目名称和对方单位进行设置
            DynamicObject outContractDy = (DynamicObject) newValue;
            //2、合同乙方
            DynamicObject partbDy = outContractDy.getDynamicObject("partb");
            if(partbDy != null){
                //查询合同乙方的内部业务单元
                DynamicObject partbInternalCompanyDy = BusinessDataServiceHelper.loadSingleFromCache(partbDy.getLong("id"), "bd_supplier", "internal_company");
                partbOrgName = partbInternalCompanyDy.getString("name");
            }

        }
        getModel().setValue(PARTB_ORG_FIELD,partbOrgName);
    }
    /**
     * 支出合同值更新
     * @param e
     * @param newValue
     */
    protected void propertyChanged_Project(PropertyChangedArgs e,Object newValue){
        //改变项目名称，删除支出合同信息和对方单位信息
        getModel().setValue(PARTB_ORG_FIELD,"");
        getModel().setValue(OUT_CONTRACT_FIELD,"");
    }
    /**
     * 支出合同F7过滤
     * @param evt
     */
    protected void beforeF7Select_OutContract(BeforeF7SelectEvent evt){
        ListShowParameter showParameter = (ListShowParameter) evt.getFormShowParameter();
        DynamicObject projectname = (DynamicObject) this.getModel().getValue(PROJECTNAME_FIELD);
        if(projectname != null){
            QFilter qFilter = new QFilter("project.id", equals, projectname.getLong("id"));
            //根据项目过滤
            showParameter.getListFilterParameter().setFilter(qFilter);
        }else {
            getView().showErrorNotification("请选择项目");
            evt.setCancel(true);
        }
    }
}
